package srvctx

import (
	"time"

	"gitee.com/mkwchecking/sr-core/domain/dao/scope"
	"gitee.com/mkwchecking/sr-core/log"
	"gitee.com/mkwchecking/sr-core/log/ilog"
	"gitee.com/mkwchecking/sr-core/net/http/session"
	"gitee.com/mkwchecking/sr-core/yerr"
	"github.com/spf13/cobra"
)

var _ IContext = (*CmdCtx)(nil)

func NewCmdCtx(cmd *cobra.Command) IContext {
	return &CmdCtx{
		cmd: cmd,
		l:   log.GetDefault().With("module", "cmd", "cmd", cmd.CommandPath()),
	}
}

type CmdCtx struct {
	cmd *cobra.Command
	l   log.Logger
}

// DaoScope implements IContext.
func (c *CmdCtx) DaoScope() scope.IScope {
	return nil
}

// Deadline implements IContext.
func (c *CmdCtx) Deadline() (deadline time.Time, ok bool) {
	return c.cmd.Context().Deadline()
}

// Done implements IContext.
func (c *CmdCtx) Done() <-chan struct{} {
	return c.cmd.Context().Done()
}

// Err implements IContext.
func (c *CmdCtx) Err() error {
	return c.cmd.Context().Err()
}

// GetSessionId implements IContext.
func (c *CmdCtx) GetSessionId() (string, error) {
	return "", nil
}

// GetSessionInfo implements IContext.
func (c *CmdCtx) GetSessionInfo() (*session.Info, error) {
	return nil, yerr.ErrBizNotImplemented.Error()
}

// Logger implements IContext.
func (c *CmdCtx) Logger() ilog.Logger {
	return c.l
}

// Session implements IContext.
func (c *CmdCtx) Session() *session.SessionManager {
	return nil
}

// SessionStore implements IContext.
func (c *CmdCtx) SessionStore() (session.Storage, error) {
	return nil, yerr.ErrBizNotImplemented.Error()
}

// TraceId implements IContext.
func (c *CmdCtx) TraceId() string {
	return ""
}

// Value implements IContext.
func (c *CmdCtx) Value(key any) any {
	return c.cmd.Context().Value(key)
}
